package org.longgang.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.longgang.bean.EducationExperience;
import org.longgang.bean.PracticalExperience;
import org.longgang.bean.Resume;
import org.longgang.bean.User;
import org.longgang.service.EducationExperienceService;
import org.longgang.service.PracticalExperienceService;
import org.longgang.service.ResumeService;
import org.longgang.service.UserService;
import org.longgang.tools.ResponseData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class UserController {

	@Autowired
	private UserService service;

	@Autowired
	private ResumeService resume;

	@Autowired
	private EducationExperienceService education;

	@Autowired
	private PracticalExperienceService practical;

//	查询所有用户
	@RequestMapping("/getUserAll")
	public Object getUserAll() {
		return service.getUserAll();
	}

//	用户注册
	@RequestMapping("/insertUser")
	public Object insertUser(HttpServletRequest request) {
		String username = request.getParameter("username");
		String password = request.getParameter("password");
		String email = request.getParameter("email");

		User user = new User(username, password, email);
		service.insertUser(user);
		Integer userid = user.getUserId();
		resume.insertResume(new Resume(userid));// 创建用户的简历
		education.insertEducationExperience(new EducationExperience(userid));// 创建用户教育经历
		practical.insertPracticalExperience(new PracticalExperience(userid));// 创建用户实习经历

		HttpSession session = request.getSession();
		session.setAttribute("uid", user.getUserId());
		System.out.println(request.getSession().getId() + "  uid=" + request.getSession().getAttribute("uid"));

		ResponseData responseData = new ResponseData(1, user.getUserId(), "");
		return responseData;
	}

//	检查用户名
	@RequestMapping("/selectUserByName")
	public Object selectUserByName(HttpServletRequest request) {
		String username = request.getParameter("username");
		Integer i = service.selectUserByName(new User(username, "", ""));

		ResponseData responseData = new ResponseData(1, i, "");
		return responseData;
	}

//	检查邮箱
	@RequestMapping("/selectUserByEmail")
	public Object selectUserByEmail(HttpServletRequest request) {
		String email = request.getParameter("email");
		Integer i = service.selectUserByEmail(new User("", "", email));

		ResponseData responseData = new ResponseData(1, i, "");
		return responseData;
	}

//	登录
	@RequestMapping("/loginUser")
	public Object loginUser(HttpServletRequest request) {
		String password = request.getParameter("password");
		String email = request.getParameter("email");
		Integer i = service.selectUser(new User("", password, email));

		HttpSession session = request.getSession();
		session.setAttribute("uid", i);
		System.out.println(request.getSession().getId() + "  uid=" + request.getSession().getAttribute("uid"));

		ResponseData responseData = new ResponseData(2, i, "");// 2 表示个人用户，用于前端识别
		return responseData;
	}

//	获取session的id
	@RequestMapping("/getUserSession")
	public Object getUserSession(HttpServletRequest request) {
		System.out.println(request.getSession().getId() + "  uid=" + request.getSession().getAttribute("uid"));

		ResponseData responseData = new ResponseData(2, request.getSession().getAttribute("uid"), "");
		return responseData;
	}

//	查询用户信息（根据Id）
	@RequestMapping("/selectUserById")
	public Object selectUserById(HttpServletRequest request) {
		Integer userid = Integer.parseInt(request.getParameter("id"));
		User user = service.selectUserById(new User(userid));
		ResponseData responseData = new ResponseData(2, user, "");
		return responseData;
	}

//	清除session会话  注销功能
	@RequestMapping("/removeSession")
	public Object removeSession(HttpServletRequest request) {
		HttpSession session = request.getSession();
		session.invalidate();
		System.out.println(request.getSession().getId() + "  uid=" + request.getSession().getAttribute("uid") + "  "
				+ request.getSession().getId() + "  cid=" + request.getSession().getAttribute("cid") + " "
				+ request.getSession().getId() + "  aid=" + request.getSession().getAttribute("aid"));

		ResponseData responseData = new ResponseData(1, "", "注销 成功！");
		return responseData;
	}

//	更新简历个人基本信息部分
	@RequestMapping("/updateResume")
	public Object updateResume(HttpServletRequest request) {
		String name = request.getParameter("name");
		String sex = request.getParameter("sex");
		String birthday = request.getParameter("birthday");
		Integer age = Integer.parseInt(request.getParameter("age"));
		String workyear = request.getParameter("workyear");
		String idcard = request.getParameter("idcard");
		String phone = request.getParameter("phone");
		String email = request.getParameter("email");
		String nativeplace = request.getParameter("nativeplace");
		String city = request.getParameter("city");
		String expectcity = request.getParameter("expectcity");
		String expectsalary = request.getParameter("expectsalary");
		String postscript = request.getParameter("postscript");

		Integer userid = (Integer) request.getSession().getAttribute("uid");
		Integer i = resume.updateResume(new Resume(userid, name, sex, age, nativeplace, birthday, workyear, phone,
				idcard, email, city, expectcity, expectsalary, postscript));

		ResponseData responseData = new ResponseData(2, i, "更新成功！");
		return responseData;
	}

//	查询当前登录用户简历个人基本信息部分
	@RequestMapping("/selectResume")
	public Object selectResume(HttpServletRequest request) {
		Integer userid = (Integer) request.getSession().getAttribute("uid");
		Resume selectResumeById = resume.selectResumeById(new Resume(userid));
		ResponseData responseData = new ResponseData(2, selectResumeById, "查询 成功！");
		return responseData;
	}

//	更新当前登录用户简历实习经历
	@RequestMapping("/updatePracticalExperience")
	public Object updatePracticalExperience(HttpServletRequest request) {
		String company = request.getParameter("company");
		String practicalposition = request.getParameter("practicalposition");
		String practicalstarttime = request.getParameter("practicalstarttime");
		String practicalendtime = request.getParameter("practicalendtime");
		String b1 = request.getParameter("b1");

		Integer userid = (Integer) request.getSession().getAttribute("uid");
		Integer i = practical.updatePracticalExperience(
				new PracticalExperience(userid, company, practicalposition, practicalstarttime, practicalendtime, b1));

		ResponseData responseData = new ResponseData(2, i, "更新成功！");
		return responseData;
	}

//	查询当前登录用户简历实习经历
	@RequestMapping("/selectPracticalExperienceById")
	public Object selectPracticalExperienceById(HttpServletRequest request) {
		Integer userid = (Integer) request.getSession().getAttribute("uid");
		PracticalExperience selectPracticalExperienceById = practical
				.selectPracticalExperienceById(new PracticalExperience(userid));
		ResponseData responseData = new ResponseData(2, selectPracticalExperienceById, "查询 成功！");
		System.out.println(selectPracticalExperienceById);
		return responseData;
	}

//	更新当前登录用户简历教育经历
	@RequestMapping("/updateEducationExperience")
	public Object updateEducationExperience(HttpServletRequest request) {
		String school = request.getParameter("school");
		String college = request.getParameter("college");
		String major = request.getParameter("major");
		String edustarttime = request.getParameter("edustarttime");
		String eduendtime = request.getParameter("eduendtime");
		String edustatement = request.getParameter("edustatement");

		Integer userid = (Integer) request.getSession().getAttribute("uid");
		Integer i = education.updateEducationExperience(
				new EducationExperience(userid, school, college, major, edustarttime, eduendtime, edustatement));

		ResponseData responseData = new ResponseData(2, i, "更新成功！");
		return responseData;
	}

//	查询当前登录用户简历教育经历
	@RequestMapping("/selectEducationExperienceById")
	public Object selectEducationExperienceById(HttpServletRequest request) {
		Integer userid = (Integer) request.getSession().getAttribute("uid");
		EducationExperience selectEducationExperienceById = education
				.selectEducationExperienceById(new EducationExperience(userid));
		ResponseData responseData = new ResponseData(2, selectEducationExperienceById, "查询 成功！");
		System.out.println(selectEducationExperienceById);
		return responseData;
	}

//	根据id查询用户简历个人基本信息部分
	@RequestMapping("/selectResumePR")
	public Object selectResumePR(HttpServletRequest request) {
		Integer userid = Integer.parseInt(request.getParameter("uid"));
		Resume selectResumeById = resume.selectResumeById(new Resume(userid));
		ResponseData responseData = new ResponseData(2, selectResumeById, "查询 成功！");
		return responseData;
	}

//	根据id查询用户简历实习经历
	@RequestMapping("/selectPracticalExperienceByIdPR")
	public Object selectPracticalExperienceByIdPR(HttpServletRequest request) {
		Integer userid = Integer.parseInt(request.getParameter("uid"));
		PracticalExperience selectPracticalExperienceById = practical
				.selectPracticalExperienceById(new PracticalExperience(userid));
		ResponseData responseData = new ResponseData(2, selectPracticalExperienceById, "查询 成功！");
		System.out.println(selectPracticalExperienceById);
		return responseData;
	}

//	根据id查询用户简历教育经历
	@RequestMapping("/selectEducationExperienceByIdPR")
	public Object selectEducationExperienceByIdPR(HttpServletRequest request) {
		Integer userid = Integer.parseInt(request.getParameter("uid"));
		EducationExperience selectEducationExperienceById = education
				.selectEducationExperienceById(new EducationExperience(userid));
		ResponseData responseData = new ResponseData(2, selectEducationExperienceById, "查询 成功！");
		System.out.println(selectEducationExperienceById);
		return responseData;
	}
}
